package top.tomxwd.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.tomxwd.service.PaymentFeignService;

/**
 * @author xieweidu
 * @createDate 2020-08-02 9:38
 */
@RestController
@Slf4j
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "paymentInfoGlobalTimeoutFallBack", commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/timeout2/{id}")
    @HystrixCommand
    public String paymentInfoTimeout2(@PathVariable("id") Integer id) {
        log.info("paymentInfoTimeout当前线程：" + Thread.currentThread().getName());
        String s = paymentFeignService.paymentInfoTimeout(id);
        log.info("结果：" + s);
        return s;
    }

    public String paymentInfoGlobalTimeoutFallBack() {
        log.info("paymentInfoTimeoutFallBack当前线程：" + Thread.currentThread().getName());
        return Thread.currentThread().getName() + "全局降级处理";
    }

    @RequestMapping("/ok/{id}")
    public String paymentInfoOk(@PathVariable("id") Integer id) {
        String s = paymentFeignService.paymentInfoOk(id);
        log.info("结果：" + s);
        return s;
    }

    @RequestMapping("/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutFallBack", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public String paymentInfoTimeout(@PathVariable("id") Integer id) {
        log.info("paymentInfoTimeout当前线程：" + Thread.currentThread().getName());
        String s = paymentFeignService.paymentInfoTimeout(id);
        log.info("结果：" + s);
        return s;
    }

    public String paymentInfoTimeoutFallBack(@PathVariable("id") Integer id) {
        log.info("paymentInfoTimeoutFallBack当前线程：" + Thread.currentThread().getName());
        return Thread.currentThread().getName() + "order返回调用超时 id = " + id;
    }

}
